#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Author: FQ
Module Description: rank_info_utils ... 
File: rank_info_utils.py
Date: 2019/2/28
"""
from pyshare.dal.repository_impl import CrossChessTeamRepository
from pyshare.const.enums.cross_chess import ChessRankKey
from common.service_config import get_app_id
from pyshare.bll.utils.cross.rank_utils import new_rankManager


class CrossChessTeamRankInfo(object):
    """
    跨服棋局队伍榜单方法类型
    """

    @classmethod
    def get_info_format(cls, team, server_id, score, form=1):
        func = getattr(cls, '_get_info_format_%s' % form, None)
        if not func:
            return
        return func(team, server_id, score)

    @staticmethod
    def _get_info_format_1(*args):
        """
        返回 [team_name, score, server_id, team_no]
        :return:
        """
        team = args[0]
        return [team.team_name, args[2], args[1], team.team_no]

    @staticmethod
    def _get_info_format_2(*args):
        """
        返回 [team_id, score, server_id]
        :param args:
        :return:
        """
        team = args[0]
        return [team.team_id, args[2], args[1]]

    @staticmethod
    def fill_leader_board_key(key, **kwarg):
        return key.format(app_id=get_app_id(), **kwarg)

    @classmethod
    def get_rank_key(cls, **kwargs):
        return cls.fill_leader_board_key(ChessRankKey.TeamScore, **kwargs)

    @classmethod
    def get_score(cls, activity_id, object_id, server_id):
        """
        获取自己的积分
        :param activity_id:
        :param object_id:
        :param server_id:
        :return:
        """
        rank_key = cls.get_rank_key(act_id=activity_id)
        rank = new_rankManager.get_rank(object_id, rank_key, server_id=server_id)
        return rank

    @classmethod
    def get_self_rank_and_score(cls, activity_id, object_id, server_id):
        """
        获取自己的排名，积分
        :param activity_id:
        :param object_id: 角色id/联盟id
        :param server_id: 区服id
        :return:
        """
        rank_key = cls.get_rank_key(act_id=activity_id)
        rank = new_rankManager.get_rank(object_id, rank_key, server_id=server_id)
        score = new_rankManager.get_score(object_id, rank_key, server_id=server_id)
        return rank, score

    @classmethod
    def get_info(cls, activity_id, rank_list, form):
        """
        获取相关信息
        :param activity_id:
        :param rank_list:
        :param form:
        :return:
        """
        team_id_list = list()
        for team_id, score, server_id in rank_list:
            team_id_list.append(team_id)

        # 获取信息
        info_dict = {t.team_id: t for t in CrossChessTeamRepository.filter_name_by_team_id_list(team_id_list, activity_id)}
        new_list = list()
        for team_id, score, server_id in rank_list:
            t = info_dict.get(team_id, None)
            if not t:
                continue
            new_list.append(cls.get_info_format(t, server_id, score, form))

        return new_list

    @classmethod
    def get_rank_list_info(cls, activity_id, start, end, form=0):
        """
        返回带信息的榜单数据
        :param activity_id:
        :param start:
        :param end:
        :param form: 信息格式
        :return:
        """
        rank_key = cls.get_rank_key(act_id=activity_id)
        rank_list = new_rankManager.get_range_by_two_value(rank_key, start=start, end=end)

        if form == 0:
            return rank_list

        return cls.get_info(activity_id, rank_list, form)

    @classmethod
    def delete_team_from_rank(cls, activity_id, team_id, server_id):
        """
        删除队伍排名
        :param activity_id:
        :param team_id:
        :param server_id:
        :return:
        """
        rank_key = cls.get_rank_key(act_id=activity_id)
        new_rankManager.del_from_rank(team_id, rank_key, server_id=server_id)

    @classmethod
    def delete_whole_rank(cls, activity_id):
        """
        清空排行榜
        :param activity_id:
        :return:
        """
        rank_key = cls.get_rank_key(act_id=activity_id)
        new_rankManager.delete_whole_rank(rank_key)